package com.wenx.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

import java.util.Date;

public class AppTimer extends RouteBuilder {

    public static void main(String[] args) throws Exception {

        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.addRoutes(new AppTimer());
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }

    //每隔2秒向控制台打印一条消息
    //myTimer名称,定时器对象和相应的线程可以共享。
    // 如果你在另一条路由中使用相同的计时器的名字,它将重用相同的定时器对象
    public void configure() throws Exception {
        from("timer://myTimer?period=2000")
                .setBody().simple("Current time is ${header.firedTime}")
                .to("stream:out");
    }
}
